class ListNode:
    """ListNode."""

    def __init__(self, val):
        self.val = val
        self.next = None

    def __str__(self):
        tmp_str = "#"
        node = self
        while node:
            tmp_str += f"->{node.val}"
            node = node.next
        return tmp_str


class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

    def _pre_order(self):
        if not self:
            return ""
        res = str(self.val)
        if self.left:
            res += self.left._pre_order()
        if self.right:
            res += self.right._pre_order()
        return res

    def _in_order(self):
        if not self:
            return ""
        res = ""
        if self.left:
            res += self.left._in_order()
        res += str(self.val)
        if self.right:
            res += self.right._in_order()
        return res

    def _post_order(self):
        if not self:
            return ""
        res = ""
        if self.left:
            res += self.left._post_order()
        if self.right:
            res += self.right._post_order()
        res += str(self.val)
        return res

    def order(self, mode="pre_order"):
        """the string when call print(TreeNOde).

        Args:
            mode: must be one of those:
            ["pre_order","in_order","post_order"]
        """
        tmp_str = "#"
        if mode == "pre_order":
            tmp_str += self._pre_order()
        elif mode == "in_order":
            tmp_str += self._in_order()
        elif mode == "post_order":
            tmp_str += self._post_order()
        else:
            raise ValueError(r"mode must be in [pre_order,in_order,post_order]")
        return tmp_str
